<?php

namespace App\Api\Logic;

use App\Api\Logic\Validate\WithdrawalValidate;
use App\Exceptions\ValidationException;
use App\Helpers\ResponseEnum;
use App\Models\TongUser;
use App\Models\TongUserBank;
use App\Models\TongUserWithdrawal;

class WithdrawalLogic extends BaseLogic
{

    /**
     * 获取对象实力
     * @return WithdrawalValidate
     */
    private function getValidate():WithdrawalValidate{
        return  new WithdrawalValidate();
    }


    /**
     * 用户申请提现
     * @return void
     */
    public function applyWithdrawal($data){
        $validate = $this->getValidate();
        //throw new ValidationException(ResponseEnum::HTTP_ERROR,'系统升级，请先核对自己的数据');
        if(!$validate->scene('applyWithdrawal')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
        $query = TongUserBank::query()->where('id',$data['bank_id'])->where('u_id',$data['u_id']);
        if(!$query->exists()){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'银行卡信息错误');
        }
        //判断用户是否有申请中的提现
        $wQuery = TongUserWithdrawal::query()->where('u_id',$data['u_id'])->where('status',1);
        if($wQuery->exists()){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'您有审核中的提现,请审核通过后再次发起提现');
        }
        if($data['money']<1){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'最小提现金额1元');
        }
        //判断用户的钱包余额
        $userInfo = TongUser::query()->where('id',$data['u_id'])->first();
        if($data['money']>$userInfo['money']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'提现金额不能大于钱包余额');
        }

    }

    /**
     * @param array $data
     * @return void
     */
    public function getWithdrawalList(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getWithdrawalList')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
    }

}
